package com.jingan.nursinghome.regulatory.separate

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.Rect
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import cn.android.support.v7.lib.sin.crown.base.BaseActivity
import cn.android.support.v7.lib.sin.crown.kotlin.anko.Titlebar
import cn.android.support.v7.lib.sin.crown.kotlin.base.BaseDialog
import cn.android.support.v7.lib.sin.crown.kotlin.common.Toast
import cn.android.support.v7.lib.sin.crown.kotlin.common.px
import com.jingan.nursinghome.regulatory.separate.bean.tmfl
import org.jetbrains.anko.*
import org.jetbrains.anko.recyclerview.v7.recyclerView
import org.jetbrains.anko.sdk25.coroutines.onClick

/**
 * Created by 彭治铭 on 2018/6/28.
 */
class CreateSeparateDailog2(var activity: BaseActivity, var xjid: String, var pid: String) : BaseDialog(activity) {//xjid 巡检列表信息id

    var recyclerView: RecyclerView? = null
    override fun onCreateView(context: Context): View? {
        return context.UI {
            verticalLayout {
                backgroundColor = Color.parseColor("#F2F2F2")
                var topbar = Titlebar.topBar(context as Activity, "新建千分巡检", Color.parseColor("#666666"))
                topbar.view?.setBackgroundColor(Color.WHITE)
                topbar.textColor = Color.BLACK
                topbar.shadow = true
                topbar.leftTextView?.onClick {
                    dismiss()
                }
                addView(topbar.view)
                relativeLayout {
                    recyclerView = recyclerView {
                        var linearLayoutManager = LinearLayoutManager(owner)
                        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
                        layoutManager = linearLayoutManager
                    }.lparams {
                        width = matchParent
                        height = matchParent
                        topOf(px.id("zuoti"))
                    }
                    textView {
                        id = px.id("zuoti")
                        gravity = Gravity.CENTER
                        backgroundColor = Color.parseColor("#00CCFF")
                        text = "开始评分"
                        textSize = px.textSizeX(32)
                        textColor = Color.WHITE
                        onClick {
                            //fixme 开始评分
                            adapter?.let {
                                var tmflname: String? = ""
                                list.forEach {
                                    if (it.ischcheck && it.type.equals("0")) {
                                        if (tmflname.equals("")) {
                                            tmflname = it.fl_name
                                        } else {
                                            tmflname = tmflname + "," + it.fl_name
                                        }
                                    }
                                }
                                tmflname?.let {
                                    if (it.trim().length > 0) {

                                        var count = 0

                                        //历史整改
                                        Uri.history(activity, pid, it, success = {
                                            count++
//                                            if (Uri.topicinfoList.size > 0 && count > 1) {
//                                                SeparateTableOSDialog.getInstance(activity, pid)
//                                                dismiss()
//                                            }
                                        }, fail = {
                                            //Toast.show(it)
                                            //update()
                                        })

                                        //千分表数据
                                        Uri.qfb(activity, pid, it, success = {
                                            count++
                                            if (Uri.topicinfoList.size > 0) {
                                                //千分表评分系统
                                                SeparateTableOSDialog.getInstance(activity, pid)
                                                dismiss()
                                            } else {
                                                if (Uri.topicinfoList.size <= 0) {
                                                    Toast.show("题目为空")
                                                }
                                                update()
                                            }
                                        }, fail = {
                                            Toast.show(it)
                                            update()
                                        })
                                    } else {
                                        Toast.show("请选择题目类型")
                                    }
                                }

                            }
                        }
                    }.lparams {
                        width = matchParent
                        height = px.y(100)
                        alignParentBottom()
                    }
                }.lparams {
                    width = matchParent
                    height = matchParent
                }
            }
        }.view
    }

    init {
        isDarkmode(true).isDismiss(false)
        setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.crown_window_right)
    }

    var list = mutableListOf<tmfl>()
    var adapter: MyAdapter? = null
    override fun listener() {
        adapter = MyAdapter(list)
        recyclerView?.adapter = adapter
        adapter?.notifyDataSetChanged()
        recyclerView?.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect?, itemPosition: Int, parent: RecyclerView?) {
                super.getItemOffsets(outRect, itemPosition, parent)
                var last = list.size - 1
                if (last < 0) {
                    last = 0
                }
                if (itemPosition == last) {
                    outRect?.bottom = px.x(24)
                } else {
                    outRect?.bottom = px.x(0)
                }
            }
        })
        update()
    }

    override fun recycleView() {
    }

    fun update() {
        Uri.fktmfl(activity, xjid) {
            list.clear()
            list.addAll(it)
            recyclerView?.adapter?.notifyDataSetChanged()
        }
    }

    class MyViewholder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val type: TextView by lazy { itemView.findViewById<TextView>(px.id("type")) }
        val check: CheckBox by lazy { itemView.findViewById<CheckBox>(px.id("check")) }
    }

    class MyAdapter(var list: MutableList<tmfl>) : RecyclerView.Adapter<MyViewholder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewholder {
            var view = parent.context.UI {
                frameLayout {
                    topPadding = px.x(24)
                    verticalLayout {
                        gravity = Gravity.CENTER
                        backgroundColor = Color.WHITE
                        relativeLayout {
                            textView {
                                id = px.id("type")
                                textSize = px.textSizeX(36)
                            }.lparams {
                                centerInParent()
                            }
                            checkBox {
                                id = px.id("check")
                                textSize = px.textSizeX(36)
                            }.lparams {
                                centerInParent()
                                alignParentRight()
                                rightMargin = px.x(50)
                            }
                        }.lparams {
                            width = px.realWidth().toInt()
                            height = px.x(200)
                        }
                    }
                }
            }.view
            return MyViewholder(view)
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: MyViewholder, position: Int) {
            holder.type.setText(list[position].fl_name)
            holder.check.onClick {
                list[position].ischcheck = holder.check.isChecked
                if (list.size > 1) {
                    notifyItemRangeChanged(0, list.size)
                } else {
                    notifyItemRangeChanged(0, 0)
                }
            }
            if (list[position].type.equals("1")) {
                //服务器已选中
                holder.type.setTextColor(Color.GRAY)//灰色
                holder.check.isChecked = true
                holder.check.isEnabled = false
            } else if (list[position].type.equals("0")) {
                holder.check.isEnabled = true
                if (list[position].ischcheck) {
                    //选中
                    holder.type.setTextColor(Color.parseColor("#70E2FF"))
                    holder.check.isChecked = true
                } else {
                    //未选中
                    holder.type.setTextColor(Color.BLACK)
                    holder.check.isChecked = false
                }
            }

        }
    }
}